var app = getApp();
var user = require('../../../../util/user.js');
var util = require('../../../../util/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: 0,
    order:{
      id:0,
      orderNo:"",
      userId:0,
      userName:"",
      prodName:"",
      orderPrice:"",
      number:0,
      orderStatus:0,
      createdAt:""
    },
    fileSrc:""
  },
  hasLogin:false,
  flag: false,


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId
    });
    if (!app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/page/auth/login/login"
      });
    }
  },
  order: function (url, token) {
    var that = this;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        "Content-Type": "json",
        'X-AUTH-TOKEN': token
      },
      success: function (res) {
        console.log(res.data);    //控制台打印通过API获取的object对象，可展开查看结构
        that.setData({
          order: res.data
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.hasLogin) {
      var token = wx.getStorageSync('token');
      this.setData({
        hasLogin: true,
      });

      var that = this;  //把this对象复制到临时变量that
      var orderUrl = app.globalData.apiBase + app.globalData.orderDetailUrl + "?orderId=" + this.data.orderId;
      console.log(orderUrl);
      this.order(orderUrl, token);
    } else {
      wx.removeStorageSync('userInfo');
      wx.removeStorageSync('token');
    }
  },
  // “去付款”按钮点击效果
  goDownLoad: function() {
    let that = this;
    var token = wx.getStorageSync('token');
    var downloadUrl = app.globalData.apiBase + app.globalData.downloadUrl+"?order_id="+that.data.orderId;
    console.log(downloadUrl+",token="+token);

    wx.downloadFile({
      url: downloadUrl,
      header: {
        "Content-Type": "application/x-zip-compressed",
        "X-AUTH-TOKEN": token
      },
      success(res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if(res.data.retCode==-1){
          try {
            wx.removeStorageSync('userInfo');
            wx.removeStorageSync('token');
          } catch (e) {
            // Do something when catch error
          }
          wx.navigateTo({
            url: "/page/auth/login/login"
          });
        }
        if (res.statusCode === 200) {
          console.log('downloadFile success, info is:', res);
          var filePath = res.tempFilePath;
            wx.addPhoneContact({
              firstName: '微信-测试号码',//联系人姓名
              mobilePhoneNumber: '15906673934',//联系人手机号
            })
        }
      },
      fail: function({errMsg}) {
        console.log('downloadFile fail, err is:', errMsg)
      },
      complete: function () {
        that.setData({
         
        })
      }
    })


    // wx.request({
    //   url: downloadUrl,
    //   method: 'GET',
    //   header: {
    //     "Content-Type": "json",
    //     'X-AUTH-TOKEN': token
    //   },
    //   success: function (res) {
    //     console.log(res.data);    //控制台打印通过API获取的object对象，可展开查看结构
    //   }
    // });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})